//
//  MyCommunityViewController.m
//  cityStar
//
//  Created by joyban on 15-3-4.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "MyCommunityViewController.h"
#import "MyCommunityCell.h"
#import "UIColor+FlatUI.h"
#import "UserCityModel.h"
#import "SearchCommunityViewController.h"
#import "UIScrollView+SVPullToRefresh.h"
NSString *const MyCommunityCellID = @"MyCommunityCellID";

@interface MyCommunityViewController ()<CSCoumnityDelegate>
@property (weak, nonatomic) IBOutlet UITableView *MyCommunityTabelview;
@property (nonatomic, strong) NSMutableArray *arrayRequest;
@property (weak, nonatomic) IBOutlet UILabel *nolabel;
@property (weak, nonatomic)  UIButton *cancelbtn;


@end

@implementation MyCommunityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的社区";
    [self addBackButton];
    [self setUpTableViews];
    self.nolabel.hidden = YES;
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithTitle:@"社区绑定" actionBlock:^{
        SearchCommunityViewController *searchVC = [[SearchCommunityViewController alloc]init];
        [weakSelf.navigationController pushViewController:searchVC animated:YES];
    }];
    [self.MyCommunityTabelview addPullToRefreshWithActionHandler:^{
        [self webRequest_GetMyUserCity];
    }];
    [self webRequest_GetMyUserCity];

}

- (void)webRequest_GetMyUserCity
{
    [HttpRequestManager getMethod:KinterFace_MyUserCity parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        NSLog(@"%@",response);
        if (success) {
            self.arrayRequest = [[NSMutableArray alloc]init];
            [self.arrayRequest addObjectsFromArray:response[@"value"][@"content"]];
            if(self.arrayRequest.count  == 0)
            {
                self.nolabel.hidden = NO;
            }else
            {
                self.nolabel.hidden = YES;
            }
            [self.MyCommunityTabelview reloadData];
        }
        [self.MyCommunityTabelview.pullToRefreshView stopAnimating];
    }];
}

-(void)setUpTableViews
{
    [self.MyCommunityTabelview registerNib:[MyCommunityCell nib] forCellReuseIdentifier:MyCommunityCellID];
}

- (void)addRightButtonWithTitle:(NSString *)title actionBlock:(void(^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 65, 20)];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor colorWithRed:0.980 green:0.450 blue:0.239 alpha:1.000] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [MyCommunityCell heightOfTableCell];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayRequest.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(self.arrayRequest.count == 0)
    {
        return 0;
    }
    return 1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImageView *imageTwo = [[UIImageView alloc]initWithFrame:CGRectMake(0, 35, tableView.bounds.size.width, 1)];
    [imageTwo setBackgroundColor:[UIColor colorFromHexCode:@"ECECEC"]];
    UIView *background = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 25)];\
    [background setBackgroundColor:[UIColor colorFromHexCode:@"FFF3E2"]];
    UILabel * label = [[UILabel alloc] init] ;
    label.frame = CGRectMake(16, 0, 200, 25);
    label.backgroundColor = [UIColor clearColor];
    label.font=[UIFont systemFontOfSize:15];
    [background addSubview:label];
    label.text = @"最多可绑定5个社区";
    label.textColor = [UIColor colorFromHexCode:@"333333"];
    
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"f6f5f1"]];
    [sectionView addSubview:imageTwo];
    [sectionView addSubview:background];
    return sectionView;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCommunityCell *CommunityCell = [tableView dequeueReusableCellWithIdentifier:MyCommunityCellID];
    UserCityModel *model = [UserCityModel objectFromDictionary:self.arrayRequest[indexPath.row]];
    [CommunityCell setDataUsercityModel:model];
    CommunityCell.CancleBtn.tag = indexPath.row;
    CommunityCell.delegate = self;
    CommunityCell.selectionStyle = UITableViewCellSelectionStyleGray;
    return CommunityCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    VideoArrayModel *model = [VideoArrayModel objectFromDictionary:self.arrayRequest[indexPath.row]];
//    VideoViewDetailViewController *videoViewController = [[VideoViewDetailViewController alloc]init];
//    videoViewController.videoModel = model;
//    [self.navigationController pushViewController:videoViewController animated:YES];
//    [tableView reloadData];
}

-(void)senderCancleAction:(UIButton *)btn
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否解除绑定" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消",nil];
    [alertView show];
//    UserCityModel *model = [UserCityModel objectFromDictionary:self.arrayRequest[btn.tag]];
    self.cancelbtn = btn;
    NSLog(@"%ld",(long)self.cancelbtn.tag);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if(buttonIndex == 0)
    {
        NSString *postUrl  = [NSString stringWithFormat:@"%@/%@",KinterFace_CancleCity,self.arrayRequest[self.cancelbtn.tag][@"id"]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            NSLog(@"%@",response);
            if (success) {
                [self webRequest_GetMyUserCity];
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
